﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Management;
using System.Threading;


namespace UDPSend
{
    class Program
    {
        public static byte[] FromHex(string hex)
        {
            hex = hex.Replace(":", "");
            byte[] raw = new byte[hex.Length / 2];
            for (int i = 0; i < raw.Length; i++)
            {
                raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
            }
            return raw;
        }

      



        static void Main(string[] args)
        {

            UdpClient sock;
            string ip="127.0.0.1";
            int port = 89;
            try
            {
                sock = new UdpClient();
                sock.Connect(ip, 89);
                do
                {
                    IPHostEntry host;
                    string localIP = "";


                    host = Dns.GetHostEntry(Dns.GetHostName());
                    foreach (IPAddress ipAdd in host.AddressList)
                    {

                        if (ipAdd.AddressFamily == AddressFamily.InterNetwork)
                        {
                            localIP = ipAdd.ToString();

                        }

                    }
                    Console.WriteLine(localIP);




                    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    string MACAddress = "";
                    foreach (ManagementObject mo in moc)
                    {
                        if (mo["MacAddress"] != null)
                        {
                            if ((bool)mo["IPEnabled"] == true)
                            {
                                MACAddress = mo["MacAddress"].ToString();
                                Console.WriteLine(MACAddress);
                            }
                        }
                    }
                       





                    string input = Console.ReadLine();


                    byte[] macAdd=FromHex(MACAddress);

                    byte[] dataSend = Encoding.Default.GetBytes(input);

                    dataSend = macAdd.Concat(dataSend).ToArray();
                    sock.Send(dataSend, dataSend.Length);
                    





                    Console.WriteLine(sock.Client.AddressFamily.ToString());
                    Console.WriteLine(sock.Client.LocalEndPoint.AddressFamily.ToString());

               

/*
                    int j = 0;
                   

                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

                    PhysicalAddress address = nics[j].GetPhysicalAddress();
                
                   



                    byte[] bytes = address.GetAddressBytes();

                    for (int i = 0; i < bytes.Length; i++)
                    {
                        Console.Write("{0}", bytes[i].ToString("X2"));

                        if (i != bytes.Length - 1)
                        {
                            Console.Write("-");
                        }
                    }

                    Console.ReadLine();
 */

                }
                while (true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

    }
}
